<?php
/**
 * @author Etienne AUDOUIN <etienne.audouin@ingesup.com>
 * @author Adrien MARIANELLI <adrien.marianelli@ingesup.com>
 * @version 1.0
 * @package models
 * @subpackage contact
 */
class Contact_model extends CI_Model {
    /**
     * Reference a la table contact
     * @access protected
     * @var string
     */
    protected $table="contact";
    /**
     *@deprecated
     */
    private $id = '';

    /**
     *@deprecated
     */
    public function getId () {
        return $this->id;
    }

    /**
     * Cree un nouveau contact
     * {@source}
     * @param string $name
     * @param string $first_name
     * @param string $mail
     * @return bool
     */
    public function create ($name, $first_name, $mail) {
        $this->db->set('name', $name);
        $this->db->set('first_name', $first_name);
        $this->db->set('mail', $mail);
        
        return $this->db->insert($this->table);
    }

    /**
     * Renvoie les informations liees a un contact
     * {@source}
     * @todo La requete ne renvoie qu'un resultat, pas besoin de renvoyer un tableau. Par exemple utiliser array_pop()
     * @param int $id
     * @return array
     */
    public function read ($id) {
        return $this->db->select('*')
                ->from($this->table)
                ->where('id', (int) $id)
                ->get()
                ->result();
    }

    /**
     * Renvoie tous les contacts
     * {@source}
     * @return array
     */
    public function readAll () {
        return $this->db->select('*')
                ->from($this->table)
                ->get()
                ->result();
    }

    /**
     * Met a jour un contact
     * {@source}
     * @param string $first_name
     * @param string $name
     * @param int  $id
     * @param string $mail
     * @return bool
     */
    public function update ($first_name, $name, $id = null, $mail = null) {
        if ($id == null || $mail == null)
            return false;
        if ($name != null)
            $this->db->set('name', $name);
        if ($first_name != null)
            $this->db->set('first_name', $first_name);
        if ($mail != null)
            $this->db->set('mail', $mail);
        
        $this->db->where('id', (int) $id);
        
        return $this->db->update($this->table);
    }

    /**
     * Supprime un contact
     * {@source}
     * @param int $id
     * @return bool
     */
    public function delete ($id) {
        return $this->db->where('id', (int) $id)->delete($this->table);
    }

    /**
     * Filtre les contacts par mail
     * {@source}
     * @param string $searchString
     * @return array
     */
    public function searchContactByMail ($searchString) {
        return $this->db->query('
        select *
        from contact
        where mail like "%'.$searchString.'%"')->result();
    }

    /**
     * Filtre les contacts par nom
     * {@source}
     * @param string $searchString
     * @return array
     */
    public function searchContactByName ($searchString) {
        return $this->db->query('
        select *
        from contact
        where name like "%'.$searchString.'%"')->result();
    }

    /**
     * Filtre les contacts par prenom
     * {@source}
     * @param string $searchString
     * @return array
     */
    public function searchContactByFirstName ($searchString) {
        return $this->db->query('
        select *
        from contact
        where first_name like "%'.$searchString.'%"')->result();
    }

    /**
     * Filtre les contacts par champs
     * {@source}
     * @param string $searchString
     * @return array
     */
    public function searchContactByField ($searchString) {
        $result = array ();
        $result['mail'] = $this->searchContactByMail ($searchString);
        $result['name'] = $this->searchContactByName ($searchString);
        $result['firstName'] = $this->searchContactByFirstName ($searchString);

        return $result;
    }

    /**
     * Filtre les contacts a partir d'un chaîne de caractere
     * {@source}
     * @param string $searchString
     * @return array
     */
    public function searchContact ($searchString){
        $contactsBy = $this->searchContactByField($searchString);
        $displayable = array ();
        foreach ($contactsBy['mail'] as $contact) {
            $displayable[$contact->id] = 
                    array (
                        'name' => $contact->name,
                        'firstName' => $contact->first_name,
                        'mail' => $contact->mail);
        }
        foreach ($contactsBy['name'] as $contact) {
            $displayable[$contact->id] = array (
                'name' => $contact->name,
                'firstName' => $contact->first_name,
                'mail' => $contact->mail);
        }
        foreach ($contactsBy['firstName'] as $contact) {
            $displayable[$contact->id] = array (
                'name' => $contact->name,
                'firstName' => $contact->first_name,
                'mail' => $contact->mail);
        }
        return $displayable;
    }

}
?>
